//
//  XPUpdateView.swift
//  ChinaDailyNews
//
//  Created by cdmac on 16/9/28.
//  Copyright © 2016年 chinadailyhk. All rights reserved.
//

import UIKit

/// 新版本提示
@objcMembers
class XPUpdateView: XPBaseView {

    //MARK: 属性
    @IBOutlet weak var filterView: UIView!
    @IBOutlet weak var contentView: UIView!
    @IBOutlet weak var labTitle: UILabel!
    @IBOutlet weak var labNotes: UILabel!
    @IBOutlet weak var btnCancel: UIButton!
    @IBOutlet weak var btnUpdate: UIButton!
    
    @IBOutlet weak var cancelWidth: NSLayoutConstraint!
    @IBOutlet weak var cancelRight: NSLayoutConstraint!
    
    private var notes:String?
    private var downloadUrl:String?
    
    //MARK: 初始化方法
    override func initData() {
        
    }
    
    override func initSubViews() {
        super.initSubViews()
    
        self.contentView.cornerRadius = 8
        self.btnCancel.cornerRadius = 6
        self.btnCancel.borderColor = pp_color(.C159F48)
        self.btnCancel.borderWidth = 1
        self.btnUpdate.cornerRadius = 6
    
        if let str = self.notes {
            self.labNotes.text = str
        }else{
            self.labNotes.text = XPApplication.shared.appStoreInfo?.releaseNotes
        }
        labTitle.text = "AppNewVer_Title".localized
        
        btnCancel.setTitle("MT_Ignore".localized, for: .normal)
        btnUpdate.setTitle("MT_Update".localized, for: .normal)
        
        pp_alert_show(self.contentView)
    }
    
    //MARK: 自定义方法
    func customInfo(_ isForce:Bool? = false, _ notes:String? = "Thanks for using NNScores, please download the latest version for the latest experience!", _ downloadUrl:String? = AppStoreDownUrl){
        self.notes = notes
        self.downloadUrl = downloadUrl
        labNotes.text = notes 
        btnCancel.isHidden = isForce!
        
        if downloadUrl?.contains("https://testflight.apple.com") == true {
            btnCancel.setTitle("MT_Skip".localized)
            btnUpdate.setTitle("AppNewVer_JoinTest".localized)
            labTitle.text = "AppNewVer_Title2".localized
        }
        
        if isForce == true {
            cancelWidth.constant = 0
            cancelRight.constant = 0
        }else{
            cancelWidth.constant = 115
            cancelRight.constant = 10
            
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(cancelAction(_:)))
            tapGesture.numberOfTapsRequired = 2
            filterView.addGestureRecognizer(tapGesture)
        }
        self.updateConstraintsIfNeeded()
    }

    @IBAction func cancelAction(_ sender: Any) {
        pp_alert_hide(self.contentView)
    }
    
    @IBAction func updateAction(_ sender: Any) {
        umengLogEvent("version_updates")
        firebaseLogEvent("Upgrade")
        let url = URL(string:self.downloadUrl!)!
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}
